#define _CRT_SECURE_NO_WARNINGS 1 
#include"SeqList.h"
void SLInit(SL* ptr)
{
	ptr->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
	if (ptr->a == NULL)
	{
		perror("malloc");
		return;
	}
	ptr->size = 0;
	ptr->capacity = 4;
}

void SLDestory(SL* ptr)
{
	free(ptr->a);
	ptr->a = NULL;
	ptr->size = 0;
	ptr->capacity=0;
	
}

void SLCheckCapacity(SL* ptr)
{
	if (ptr->size == ptr->capacity)
	{
		SLDatatype* tmp = (SLDatatype*)realloc(ptr->a, sizeof(SLDatatype) * ptr->capacity*2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ptr->a = tmp;
		ptr->capacity *= 2;
	}
}
void SLPushBack(SL* ptr, SLDatatype x)
{
	/*SLCheckCapacity(ptr);
	ptr->a[ptr->size] = x;
	ptr->size++;*/
	SLInsert(ptr, ptr->size, x);
}

void SLPushFront(SL* ptr, SLDatatype x)
{
	/*SLCheckCapacity(ptr);
	int end = ptr->size-1;
	while(end>=0)
	{
		ptr->a[end+1] = ptr->a[end];
		end--;
	}
	ptr->a[0] = x;
	ptr->size++;*/
	SLInsert(ptr, 0, x);
}

void SLPopBack(SL* ptr)
{
	/*if (ptr->size == 0)
	{
		return;
	}*/
	assert(ptr->size > 0);	
	ptr->size--;
}

void SLPopFront(SL* ptr)
{
	assert(ptr->size > 0);
	int start = 0;
	while (start < ptr->size - 1)
	{
		ptr->a[start] = ptr->a[start + 1];
		start++;
	}
	ptr->size--;
}

void SLInsert(SL* ptr, int pos,SLDatatype x)
{
	assert(0 <= pos && pos <=ptr->size);
	SLCheckCapacity(ptr);
	int end = ptr->size-1 ;
	while (end >= pos)
	{
		ptr->a[end+1] = ptr->a[end ];
		end--;
	}
	ptr->a[pos] = x;
	ptr->size++;
}

void SLErase(SL* ptr, int pos)
{
	assert(0 <= pos && pos < ptr->size);
	/*assert(ptr->size > 0);*/
	int start = pos + 1;
	while (start < ptr->size) 
	{
		ptr->a[start-1] = ptr->a[start];
		start++;
	}
	ptr->size--;
}
 
void SLPrint(SL* ptr)
{
	for (int i = 0; i < ptr->size; i++)
	{
		printf("%d ", ptr->a[i]);
	}
	printf("\n");
}

int SLFind(SL* ptr, SLDatatype x)
{
	for (int i = 0; i < ptr->size; i++)
	{
		if (ptr->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SLModify(SL* ptr, int pos, SLDatatype x)
{
	assert(0 <= pos && pos < ptr->size);
	ptr->a[pos] = x;
}